//
//  YLCustomTabBarController.m
//  InduceTool
//
//  Created by 魏延龙 on 2021/12/12.
//  Copyright © 2021 魏延龙. All rights reserved.
//

#import "YLCustomTabBarController.h"
#import "YLCustomVC.h"
@interface YLCustomTabBarController ()

@property (nonatomic, strong) UIView *customView;
@property (nonatomic, weak) UIButton *selectedBtn;
@property (nonatomic, strong) NSMutableArray *barbtns;
@end

@implementation YLCustomTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupVCs];
    [self setupTabbar];
}

-(void)viewWillLayoutSubviews {
    CGRect rect = self.tabBar.frame;
    NSLog(@"rect viewWillLayoutSubviews %@",NSStringFromCGRect(rect));
    self.customView.frame = rect;
    [self updateBtnsLayout];
}

-(void)setupVCs {
    YLCustomVC *redViewController = [[YLCustomVC alloc] init];
    redViewController.view.backgroundColor  = [UIColor redColor];
    redViewController.tabBarItem.title = @"red";
    
    YLCustomVC *greenViewController = [[YLCustomVC alloc] init];
    greenViewController.view.backgroundColor = [UIColor greenColor];
    greenViewController.tabBarItem.title = @"green";
    
    self.viewControllers = @[redViewController,greenViewController];
}
-(void)setupTabbar {
    self.tabBar.hidden = YES;
    
    CGRect rect = self.tabBar.frame;
    NSLog(@"rect setup %@",NSStringFromCGRect(rect));

    [self.view addSubview:self.customView];
    
    
    for (int i = 0; i < self.viewControllers.count; i++) {
        NSString *titleStr = self.viewControllers[i].tabBarItem.title;
        UIButton *button = [[UIButton alloc] init];
        [button setTitle:titleStr forState:UIControlStateNormal];
        [self.customView addSubview:button];
        //设置按钮的标记, 方便来索引当前的按钮,并跳转到相应的视图
        button.tag = i;
        [button addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        //设置初始显示界面
        if (0 == i) {
            button.selected = YES;
            self.selectedBtn = button;  //设置该按钮为选中的按钮
        }
        [self.barbtns addObject:button];
    }
    
}

-(void)updateBtnsLayout {
    for (int i = 0; i < self.barbtns.count; i ++) {
        UIButton *button = [self.barbtns objectAtIndex:i];
        CGFloat x = i * self.customView.frame.size.width / self.barbtns.count;
        button.frame = CGRectMake(x, 0, self.customView.frame.size.width / 2, self.customView.frame.size.height);
    }
}

//TabBar点击，切换界面
- (void)clickBtn:(UIButton *)button {
    //1.先将之前选中的按钮设置为未选中
    self.selectedBtn.selected = NO;
    //2.再将当前按钮设置为选中
    button.selected = YES;
    //3.最后把当前按钮赋值为之前选中的按钮
    self.selectedBtn = button;
    //4.跳转到相应的视图控制器. (通过selectIndex参数来设置选中了那个控制器)
    self.selectedIndex = button.tag;
}


-(UIView *)customView {
    if (!_customView) {
        _customView = [[UIView alloc] init];
        _customView.backgroundColor = [UIColor cyanColor];
    }
    return _customView;
}

-(NSMutableArray *)barbtns {
    if (!_barbtns) {
        _barbtns = [NSMutableArray array];
    }
    return _barbtns;
}
@end
